package com.example.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.example.weather.*;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Sqlitetest extends Activity {
	private Button btn_create;
	private Button btn_insert;
	private Button btn_query;
	private Button btn_update;
	private Button btn_delete;
	private DbHelper dbhelper;
	private SQLiteDatabase 	db;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlitetest);
        dbhelper = new DbHelper(Sqlitetest.this,	 "weather");
        btn_create = (Button)findViewById(R.id.btn_create);
        btn_insert = (Button)findViewById(R.id.btn_insert);
        btn_query = (Button)findViewById(R.id.btn_query);
        btn_update = (Button)findViewById(R.id.btn_update);
        btn_delete = (Button)findViewById(R.id.btn_delete);
        btn_create.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			    
				//dbhelper.getReadableDatabase();
				InputStream is	=getResources().openRawResource(R.raw.weather);
				String dbdir = "/data/data/com.example.weather/databases/";
				File dir = new File(dbdir);
					if(!dir.exists()){
						dir.mkdir();
						System.out.println("mkdir:"+dbdir);
					}
					String dbname=dbdir +"weather";
					File dbfile = new File(dbname);
					try{
							FileOutputStream fos = new FileOutputStream(dbfile);
							byte []bytes = new byte[4000];
							int count =0;
							while((count=is.read(bytes))>0){
								fos.write(bytes);
							}
							is.close();
							fos.close();
					}catch(FileNotFoundException e){
						
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			 
			}
		});
        btn_insert.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				db =  dbhelper.getReadableDatabase();
				ContentValues values= new ContentValues();
				values.put("id","111111111111");
				values.put("name", "liuyuheng");
				db.insert("student", null, values);
				
			}
		});
        btn_query.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
			 db=dbhelper.getReadableDatabase(); 
			 Cursor cursor = db.rawQuery("select * from weather", null);
			 while(cursor.moveToNext()){
				 String id =cursor.getString(0);
				 String name=cursor.getString(1);
				 System.out.println(id);
				 System.out.println(name);
				 
			 }
			 cursor.close();
			 
			
		}
	});
        btn_update.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
			db=dbhelper.getReadableDatabase();
		}
	});
        btn_delete.setOnClickListener(new OnClickListener() {
	  
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			
			db=dbhelper.getReadableDatabase();
		}
	});
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}
